﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.Security.Cryptography;
using System.Text;

public partial class Profile_ : System.Web.UI.Page
{
    string logo="default.jpeg";
    int logodefault = 0;
    public static string tempo;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack)
        {

            tempo = NewPass.Text;

        }

        else
        {
            NewPass.Text = tempo;
            repass.Text = tempo;

        }


    }
   
    //boton submit
   
    private string crearXml()
    {
        string sxml = "";
        sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <datos_registro><user>";
        sxml = sxml +  user.Text;
        sxml = sxml + "</user><pass>" + NewPass.Text;
        sxml = sxml + "</pass><firstname>" + firstname.Text + "</firstname>";
        sxml = sxml + "<lastname>"+lastname.Text +"</lastname>";
        sxml = sxml + "<email>" + email.Text + "</email>";
        sxml = sxml + "<comp-name>" + cbCompany.Text + "</comp-name>";
        sxml = sxml + "<title>" + title.Text + "</title>";
         sxml = sxml + "<imagen>//data//logos//default.jpeg</imagen>";
        sxml = sxml + "<direcciones>";

        //para profile
        sxml = sxml + "<direccion><add1>" + paddress1.Text + "</add1>";
        sxml = sxml + "<add2>" + paddress2.Text + "</add2>";
        sxml = sxml + "<city>" + pcity.Text + "</city>";
        sxml = sxml + "<state>" + cbPState.Text + "</state>";
        sxml = sxml + "<zip>" + pcity.Text + "</zip>";
        sxml = sxml + "<office-phone>" + officephone.Text + "</office-phone>";
        sxml = sxml + "<cell-phone>" + cellphone.Text + "</cell-phone>";
        sxml = sxml + "<fax>" + Fax.Text + "</fax>";
        sxml = sxml + "<tipo>1</tipo>";
        sxml = sxml + "<credit_card>";
        sxml = sxml + "<namecreditcard>"+namecreditcard.Text+"</namecreditcard>";
        sxml = sxml + "<creditcard>"+creditcard.Text+"</creditcard>";
        sxml = sxml + "<expdate>"+ExpDate.Text+"</expdate>";
        sxml = sxml + "<ccv>"+cvv.Text+"</ccv>";
        sxml = sxml + "</credit_card></direccion>";

        //Dir Billing

        sxml = sxml + "<direccion><add1>" + baddress1.Text + "</add1>";
        sxml = sxml + "<add2>" + baddress2.Text + "</add2>";
        sxml = sxml + "<city>" + bcity.Text + "</city>";
        sxml = sxml + "<state>" + cbBState.Text + "</state>";
        sxml = sxml + "<zip>" + bzip.Text + "</zip>";
        sxml = sxml + "<office-phone></office-phone>";
        sxml = sxml + "<cell-phone></cell-phone>";
        sxml = sxml + "<fax></fax>";
        sxml = sxml + "<tipo>2</tipo>";
        sxml = sxml + "<tipo></tipo>";
        sxml = sxml + "<credit_card>";
        sxml = sxml + "<namecreditcard>" + namecreditcard.Text + "</namecreditcard>";
        sxml = sxml + "<creditcard>" + creditcard.Text + "</creditcard>";
        sxml = sxml + "<expdate>" + Fax.Text + "</expdate>";
        sxml = sxml + "<ccv>" + cvv.Text + "</ccv>";
        sxml = sxml + "</credit_card></direccion>";
        sxml = sxml + "</direcciones>";
        sxml = sxml + "</datos_registro>";
       
        return sxml;
    }
    
   
   
    
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        
        if (validar()==1)
        {
            string sxml = crearXml();
            //consumir webservice enviando sxml

            //creo temporalmente mi respuesta
                       
           
            String datos = "<?xml version=\"1.0\" encoding=\"utf-8\"?><datos_respuesta>  <respuestaid> 1</respuestaid>";
            datos = datos + "<usuarioid>23</usuarioid><nombre>astrid</nombre> <apellido>bolanos</apellido> <alias>lucianab</alias>";
            datos = datos + " <perfilid>admin</perfilid><permiso>1111111</permiso><imagen>.//data/logos//default.jpeg</imagen></datos_respuesta>";
            rUser rreg = Utility.getRUser(datos);
            if (rreg.getRespuestaId() == 1)
            {
                
                guardarlogo(rreg);
                
                //enviar mail
                string from = "newclient@bpophoto.com";
                string to = email.Text;
                string subject = "Welcome to bpophotos.com";
                string body = "<div style=\"color:Blue\">   Welcome to <img src=\"http://localhost:1896/BPOPhotos/img/bpophotoorange.png \" alt=\"\" width=\"200\"></div>";
                body = body + " Thank you for signing up with BPOPhotos.com ";
                body = body + " you're just one click away from working with ";
                body = body + "our profesional staff";
               // body = body + "<br />Please click <a href=\"www.gmail.com" + rreg.getUsuarioId() + " \"> here </a> to activate your account and start taking advantage of our services";
                body = body + "<br />Please click <a href=\"http://localhost:1896/BPOPhotos/useractivate.aspx?id=" + rreg.getUsuarioId() + " \"> here </a> to activate your account and start taking advantage of our services";
                string result = Utility.sendMail(from, to, subject, body);
                if (result.Equals("ok"))
                {
                    Response.Redirect("CRegistro.aspx");
                }


            }
           
            


        }
    }

    private int validar()
    {
        int result = 1;
        if (File1.PostedFile != null) { logodefault = 4; }
        if (email.Equals("")) { result = result * 0; }
        if (cbBState.SelectedIndex == 0) { result = result * 0; }
        if (cbPState.SelectedIndex == 0) { result = result * 0; }
        if (NewPass.Equals(repass.Text)) { result = result * 0; }
        if (logodefault==0 || logodefault>4){result=result*0;}
        return result;
        
    }

    private void guardarlogo(rUser rreg)
    {
       
        if (File1.PostedFile != null)
        {
            try
            {
                
                
                string filepath = File1.PostedFile.FileName;
                                            
                File1.PostedFile.SaveAs(Server.MapPath(".//data/logos//U"+rreg.getUsuarioId()+"_"+filepath) );
               //update logo for user WS

            }
            catch (Exception ex)
            {
                Span3.InnerHtml = "Error saving file <b>"+File1.PostedFile.FileName;
                  
            }
        }
       
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked==true){
            CheckBox2.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 1;
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox3.Checked = false;
            logodefault = 2;
        }
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox3.Checked == true)
        {
            CheckBox1.Checked = false;
            CheckBox2.Checked = false;
            logodefault = 3;
        }
    }
    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox4.Checked == true)
        {
            baddress1.Text = paddress1.Text;
            baddress2.Text = paddress2.Text;
            bcity.Text = pcity.Text;
            cbBState.SelectedValue = cbPState.SelectedValue;
            bzip.Text = pzip.Text;
        }
        else {
            baddress1.Text = "";
            baddress2.Text = "";
            bcity.Text = "";
            cbBState.SelectedIndex=0;
            bzip.Text = "";
        
        }
    }
    protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
    {
        logodefault = 4;
    }

    protected void office1_TextChanged(object sender, EventArgs e)
    {

        if (officephone.Text.Length == 10)
        {
            string toffice;


            toffice = "(" + officephone.Text[0].ToString() + officephone.Text[1].ToString() + officephone.Text[2].ToString() + ")"
                + officephone.Text[3].ToString() + officephone.Text[4].ToString() + officephone.Text[5].ToString() + "-"
                + officephone.Text[6].ToString() + officephone.Text[7].ToString() + officephone.Text[8].ToString() + officephone.Text[9].ToString();
            officephone.Text = toffice;
            RegularExpressionValidator6.Enabled = false;

        }
        else
        {
            if (officephone.Text.Length != 13)
            {
                RegularExpressionValidator6.Enabled = true;

            }

        }






    }
    protected void cellphone_textchanged(object sender, EventArgs e)
    {
        if (cellphone.Text.Length == 10)
        {
            string toffice;



            toffice = "(" + cellphone.Text[0].ToString() + cellphone.Text[1].ToString() + cellphone.Text[2].ToString() + ")"
                + cellphone.Text[3].ToString() + cellphone.Text[4].ToString() + cellphone.Text[5].ToString() + "-"
                + cellphone.Text[6].ToString() + cellphone.Text[7].ToString() + cellphone.Text[8].ToString() + cellphone.Text[9].ToString();
            cellphone.Text = toffice;

            RegularExpressionValidator7.Enabled = false;

        }
        else
        {
            if (cellphone.Text.Length != 13)
            {
                RegularExpressionValidator7.Enabled = true;

            }
        }
    }
    protected void faxphone_textchanged(object sender, EventArgs e)
    {

        if (
            Fax.Text.Length == 10

                   )
        {
            string toffice;
            toffice = "(" + Fax.Text[0].ToString() + Fax.Text[1].ToString() + Fax.Text[2].ToString() + ")    "
                + Fax.Text[3].ToString() + Fax.Text[4].ToString() + Fax.Text[5].ToString() + "-"
                + Fax.Text[6].ToString() + Fax.Text[7].ToString() + Fax.Text[8].ToString() + Fax.Text[9].ToString();
            Fax.Text = toffice;
            RegularExpressionValidator8.Enabled = false;

        }
        else
        {
            if (Fax.Text.Length != 13)
            {
                RegularExpressionValidator8.Enabled = true;

            }

        }
    }

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        lboldpass.CssClass = "style14";
        OldPass.CssClass = "txtForm";
        OldPass.Enabled = true;

        lbnewpass.CssClass = "style14";
        NewPass.CssClass = "txtForm";
        NewPass.Enabled = true;

        lblrepass.CssClass = "style14";
        repass.CssClass = "txtForm";
        repass.Enabled = true;
        


    }
    
    protected void btnchangePict(object sender, System.EventArgs e)
    {
               

    }

         

    protected void compname_TextChanged(object sender, System.EventArgs e)
    {

    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        lboldpass.CssClass = "style14";
        OldPass.Enabled = true;     
        lbnewpass.CssClass = "style14";        
        NewPass.Enabled = true;
        lblrepass.CssClass = "style14";        
        repass.Enabled = true;
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {

        Label1.CssClass = "style12";
        Label1.Visible = true;        
        CheckBox1.Enabled = true;
        CheckBox2.Enabled = true;
        CheckBox3.Enabled = true;
        Image2.Enabled = true;
        Image3.Enabled = true;
        Image4.Enabled = true;
        File1.Enabled = true;
        
    }
    protected void firstname_TextChanged(object sender, System.EventArgs e)
    {

    }
    protected void bcity_TextChanged(object sender, System.EventArgs e)
    {

    }
}
